//package com.tidc.seaserverservice.service.impl;
//
//import com.tidc.seaservermapper.dao.UserDao;
//import com.tidc.seaservermodel.exception.TidcAuthorityException;
//import com.tidc.seaservermodel.exception.TidcBadParent;
//import com.tidc.seaserverservice.service.IAuthenticationService;
//import com.tidc.seaserverservice.util.RedisUtil;
//import com.tidc.seaserverservice.util.TokenUtil;
//import com.tidc.seaserverservice.util.UserUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.HashMap;
//import java.util.UUID;
//
//
//@Service
//@Slf4j
//public class AuthenticationServiceImpl implements IAuthenticationService {
//
//    @Resource
//    private AuthenticationManager authenticationManager;
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Autowired
//    private MyUserAuthorizationServiceImpl myUserAuthorizationService;
//    @Resource
//    private TokenUtil tokenUtil;
//    @Resource
//    private UserDao userDao;
//    @Resource
//    private PasswordEncoder passwordEncoder;
//    @Override
//    public String authToken(String phone, String password) throws TidcAuthorityException, TidcBadParent {
//        try {
//            //使用用户名密码进行登录验证
//            UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(phone, password);
//            //这里会调userDetailsService
////            AuthenticationManagerDelegator.authenticate(upToken)
////            ProviderManager.authenticate(Authentication)
////            拿security所有的providerManager进行认证
//            Authentication authentication = authenticationManager.authenticate(upToken);
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//        } catch (AuthenticationException e) {
//            // 账号密码出错异常 可以将这个异常抛出不需要打日志天天输错
//            log.warn("用户名或密码错误");
//        }
//        // 获取用户信息通过认证的信息
////        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
////        UserDetails userDetails = (UserDetails) authentication.getDetails();
//        UserDetails userDetails = myUserAuthorizationService.loadUserByUsername(phone);
//        com.tidc.seaservermodel.entity.User user = userDao.queryByPhone(userDetails.getUsername());
//        UserDetails userDetailsId= new User(user.getId()+"","",true,true,true,true,userDetails.getAuthorities());
//        // 存入redis，返回key
//        String newToken  = UUID.randomUUID().toString();
//        //删除旧token 实现踢人
//        String oldToken = (String) redisUtil.get(phone);
//        if (oldToken!=null) {
//            redisUtil.delete(oldToken);
//        }
//        //存储userDetail 到redis
//        redisUtil.setObject(newToken,userDetailsId);
//        redisUtil.set(phone,newToken);
//        return newToken;
//    }
//
//}